package com.lazy.web.controller;

import com.lazy.utils.annotation.Idempotent;
import com.lazy.utils.token.client.ClientTokenUtil;
import com.lazy.utils.utils.Result;
import com.lazy.utils.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * @author zk
 * @since 2018/5/14 19:21
 */
@RestController
@RequestMapping(value = "/")
public class LoginController {

	@Autowired
	ClientTokenUtil clientTokenUtil;
	@Autowired
	HttpServletResponse response;

	@RequestMapping(value = "/testShiro",method = {RequestMethod.POST,RequestMethod.GET})
	@Idempotent(check = true, timeOut = 7, timeUnit = TimeUnit.DAYS)
	public Result testShiro(){
		return ResultUtils.objectResult("cs");
	}

	@PostMapping(value = "/signIn")
	public Result signIn(String name, String password) throws Exception {
		if("admin".equals(name) && "admin123".equals(password)){
			String token = clientTokenUtil.generateToken(1,name);
			response.setHeader("access_token",token);
			return ResultUtils.objectResult(token);
		}
		return ResultUtils.errorResult();
	}
}
